package com.ruoyi.job.task;

import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.job.kettle.KettleQuartz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component("callProcdure")
public class CallProcdure {
    private Logger logger = LoggerFactory.getLogger(CallProcdure.class);

    @Autowired
    private RedisService redisService;

    public void ryNoParams() {
        /**
         * 执行存储过程需要QMCY_PERSONAL_INFO需要如下表入库
         *   QMCY_CUST_PERSONAL_INFO_IMPORT，QMCY_CUST_DGDS，QMCY_CUST_DKMX，QMCY_CUST_KHXX，QMCY_CUST_HYCP
         */

        logger.info("开始执行存储过程定时任务===================");

        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String currentday=sdf.format(new Date(date.getTime()));

        //执行绩效短信存储过程
        String smsMsg = "proc-" + currentday + "-SHORT_MESSAGE";
        if("0".equals(redisService.getCacheObject(smsMsg))) {
            logger.info("绩效短信存储过程执行中");
        } else if("1".equals(redisService.getCacheObject(smsMsg))) {
            logger.info("绩效短信存储过程已执行完成");
        } else {
            boolean smsSend = true;
            //绩效短信
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_SBCK"))) {
                smsSend = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_ZJCT"))) {
                smsSend = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_KMBD"))) {
                smsSend = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_DKFX"))) {
                smsSend = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_CXCK"))) {
                smsSend = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_PJ"))) {
                smsSend = false;
            }
            if (smsSend) {
                redisService.setCacheObject(smsMsg, "0"); //执行中
                boolean bool = KettleQuartz.runKettleTransfer(null, "/home/ETLitem/SHORT_MESSAGE.ktr");
                if(bool) {
                    redisService.setCacheObject(smsMsg, "1"); //执行完成
                    logger.info("绩效短信存储执行完成");
                }
            } else {
                logger.info("绩效短信数据未下发完整！");
            }
        }

        //存储过程
        String proKey = "proc-" + currentday + "-QMCY_PERSONAL_INFO";
        if("0".equals(redisService.getCacheObject(proKey))) {
            logger.info("存储过程执行中");
        } else if("1".equals(redisService.getCacheObject(proKey))) {
            logger.info("存储过程已执行完成");
        } else {
            boolean isTableSuccess = true;
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_PERSONAL_INFO"))) {
                isTableSuccess = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_DGDS"))) {
                isTableSuccess = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_DKMX"))) {
                isTableSuccess = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_KHXX"))) {
                isTableSuccess = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_HYCP"))) {
                isTableSuccess = false;
            }
            if(!"1".equals(redisService.getCacheObject("authKey-" + currentday + "-QMCY_CUST_HTMX"))) {
                isTableSuccess = false;
            }

            if (isTableSuccess) {
                redisService.setCacheObject(proKey, "0"); //执行中
//                boolean bool = KettleQuartz.runKettleTransfer(null, "/home/ETLitem/CALLPRODURE.ktr");
                Map<String, String> map = new HashMap<String, String>();
                map.put("qmcy_path","/home/ETLitem");
                boolean bool = KettleQuartz.runKettleJob(map, "/home/ETLitem/CALLPRODURE_GRADE.kjb");
                if(bool) {
                    redisService.setCacheObject(proKey, "1"); //执行完成
                    logger.info("存储执行完成");
                }
            } else {
                logger.info("数据未下发完整！");
            }

        }

        logger.info("结束执行存储过程定时任务===================");

    }
}
